package com.itheima79.googleplay.ui.fragment;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.itheima79.googleplay.R;
import com.itheima79.googleplay.http.Api;
import com.itheima79.googleplay.http.HttpUtil;
import com.itheima79.googleplay.ui.widget.FlowLayout;
import com.itheima79.googleplay.util.ColorUtil;
import com.itheima79.googleplay.util.CommonUtil;
import com.itheima79.googleplay.util.DrawableUtil;
import com.itheima79.googleplay.util.JsonUtil;
import com.itheima79.googleplay.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class HotFragment extends BaseFragment {
	private ScrollView scrollView;
	private FlowLayout flowLayout;
	private int vPadding,hPadding;
	@Override
	protected View getSuccessView() {
		scrollView = new ScrollView(getActivity());
		flowLayout = new FlowLayout(getActivity());

		vPadding = CommonUtil.getDimens(R.dimen.dp6);
		hPadding = CommonUtil.getDimens(R.dimen.dp9);

		//1.设置padding值
		int padding = CommonUtil.getDimens(R.dimen.dp15);
		flowLayout.setPadding(padding, padding, padding, padding);
		//2.设置水平间距和垂直间距
		flowLayout.setHorizontalSpacing(padding);
		flowLayout.setVerticalSpacing(padding);

		scrollView.addView(flowLayout);
		return scrollView;
	}

	@Override
	protected Object requestData() {
		String result = HttpUtil.get(Api.Hot);
		final ArrayList<String> list = (ArrayList<String>) JsonUtil.parseJsonToList(result,new TypeToken<List<String>>(){}.getType());

		if(list!=null){
			CommonUtil.runOnUIThread(new Runnable() {
				@Override
				public void run() {
					//给flowLayout添加对应的子View
					for (int i = 0; i < list.size(); i++) {
						final TextView textView = new TextView(getActivity());
						textView.setTextSize(16);
						textView.setGravity(Gravity.CENTER);
						textView.setText(list.get(i));
						textView.setTextColor(Color.WHITE);
						textView.setPadding(hPadding,vPadding, hPadding, vPadding);

						Drawable pressed = DrawableUtil.generateDrawable(ColorUtil.randomColor(),hPadding);
						Drawable normal = DrawableUtil.generateDrawable(ColorUtil.randomColor(),hPadding);
						textView.setBackgroundDrawable(DrawableUtil.generateSelector(pressed, normal));

						flowLayout.addView(textView);

						//设置点击事件
						textView.setOnClickListener(new OnClickListener() {
							@Override
							public void onClick(View v) {
								ToastUtil.showToast(textView.getText().toString());
							}
						});
					}
				}
			});
		}

		return list;
	}
}
